package view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import chess.Board;
import chess.Player;
import chess.Tile;

/**
 * This class will interact with the Java console. It will take input from the
 * user and it will also display the board.
 * 
 * @author Caleb
 * 
 */
public class ConsoleView implements View {

	private Board board;

	/**
	 * This is the constructor of ConsoleView and takes a board object as a
	 * parameter
	 * 
	 * <pre>
	 * Caleb Chan
	 * -Created Feb 09, 2010
	 * </pre>
	 * 
	 * @param b
	 *            A board object used for input reference and displaying the
	 *            board
	 */
	public ConsoleView(Board b) {
		board = b;
	}

	/**
	 * This method will prompt the user to enter a coordinate and will return
	 * the tile associated with that tile on the board.
	 * 
	 * The coordinates that are entered must be three characters long. It takes
	 * the input in the * format #*# where # is any number from 0 - 8 and *
	 * which can be any character.
	 * 
	 * <pre>
	 * Caleb Chan
	 * -Created Feb 09, 2010
	 * </pre>
	 * 
	 * @return Returns a tile object associated with a spot on the board
	 */
	@Override
	public Tile chooseTile() {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		while (true) {
			String coord = null;
			try {
				System.out.print("Input coordinates (X,Y): ");
				coord = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (coord.length() == 3) {
				String xcoord = coord.substring(0, 1);// Grab the first
														// character
				String ycoord = coord.substring(2, 3);// Grab the last character
				int xCoor = Integer.parseInt(xcoord);
				int yCoor = Integer.parseInt(ycoord);
				if (xCoor < Board.BOARD_DIMENSION
						&& yCoor < Board.BOARD_DIMENSION) {
					return board.getTile(xCoor, yCoor);
				}
			}
		}
	}

	/**
	 * This method will display a text version of the chess board.
	 * 
	 * <pre>
	 * Caleb Chan
	 * -Created Feb 09, 2010
	 * </pre>
	 */
	public void displayBoard() {
		for (int i = 0; i < Board.BOARD_DIMENSION; i++) {
			System.out.print(i + " |");
			for (int j = 0; j < Board.BOARD_DIMENSION; j++) {
				if (board.getTile(j, i).isPieceHere()) {
					System.out.print("["
							+ board.getTile(j, i).getPieceHere().toString()
							+ "]");
				} else {
					System.out.print("[ ]");
				}
			}
			System.out.println("");
		}
		System.out.println("  --------------------------");
		System.out.println("    0  1  2  3  4  5  6  7");
	}

	/**
	 * In a console view, the showPossibleMoves method will simply output the
	 * list of moves and attacks
	 * 
	 * <pre>
	 * Max Joyce
	 * -Created (Feb 18, 2010)
	 * </pre>
	 */
	public void showPossibleMoves(ArrayList<chess.Tile> mTiles,
			ArrayList<chess.Tile> aTiles, chess.Tile curTile) {

		System.out.print("Current Tile: " + curTile.toString() + "  ");

		System.out.print("Moves: ");
		for (Tile tile : mTiles) {
			System.out.print(tile.toString() + ", ");
		}

		System.out.print("Attacks: ");
		for (Tile tile : aTiles) {
			System.out.print(tile.toString() + ", ");
		}
		System.out.println("");
	}

	public void showPossibleMoves(ArrayList<chess.Tile> mTiles,
			chess.Tile curTile) {
		System.out.print("Moves: ");
		for (Tile tile : mTiles) {
			System.out.print(tile.toString() + ", ");
		}

		System.out.println("");
	}

	/**
	 * This method allows the game to ask the user for a selection.
	 * 
	 * <pre>
	 * Max Joyce
	 * -Creation (Feb 18,2010)
	 * </pre>
	 * 
	 */
	public String getUserChoice(Object[] options, String question) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		String response = "";
		while (true) {
			System.out.println(question);
			System.out.print("Your choices are: ");
			for (Object o : options) {
				System.out.print(o + ", ");
			}
			System.out.println("");

			try {
				response = reader.readLine(); // Get a line
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (Object o : options) {
				if (response.equals(o)) { // If you answered one of the options
					return response; // Return it as your selection
				}
			}
		}

	}

	public void showOutputMessage(String message) {
		System.out.println(message);
	}
	
	/**
	 * These methods our various means of getting or setting text in ConsolView.
	 * 
	 * <pre>
	 * Max Joyce
	 * -Creation (Feb 18,2010)
	 * </pre>
	 * 
	 */
	public void printlnToDebugConsole(String message) {
		this.showOutputMessage(message);
	}

	public void clearDebugConsole() {
	}

	public void setDebugText(String message) {
		this.showOutputMessage(message);
	}

	public String getDebugConsoleText() {
		return System.out.toString();
	}
	public void closeView(){
		
	}
	public void updateCurrentPlayerTurn(Player currentPlayer){
		this.printlnToDebugConsole("");
		this.printlnToDebugConsole("Current Player : " + currentPlayer.getName());
		this.printlnToDebugConsole("----------------------");
	}
}
